package com.photohistory.hibernate.dao;
// Generated 21.03.2010 16:27:53 by Hibernate Tools 3.2.4.GA


import com.photohistory.hibernate.beans.CountryPhoto;

import java.util.List;
import java.util.Set;

import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;

/**
 * Home object for domain model class CountryPhoto.
 * @see com.photohistory.hibernate.dao.CountryPhoto
 * @author Hibernate Tools
 */
public class CountryPhotoHome {
	
    private static final Log log = LogFactory.getLog(CountryPhotoHome.class);
    private final SessionFactory sessionFactory = HibernateSessionFactory.getSessionFactory();

	public CountryPhoto get(Integer id) {
		CountryPhoto countryPhoto = null;
		try {
			Session session = HibernateSessionFactory.currentSession();
			countryPhoto = (CountryPhoto) session.load(CountryPhoto.class, id);
			
		} catch (HibernateException e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			// HibernateSessionFactory.closeSession();
		}

		return countryPhoto;
	}
	
	public List getAll() {
		List list = null;
		try {
			Session session = HibernateSessionFactory.currentSession();
			list = session.createQuery("from " + CountryPhoto.class.getName())
					.list();
		} catch (HibernateException e) {
			// throw new RuntimeException(e.getMessage());
			throw e;
		} finally {
			// HibernateSessionFactory.closeSession();
		}
		return list;
	}
	
	public void save(CountryPhoto countryPhoto) {
		try {
			Session session = HibernateSessionFactory.currentSession();
			HibernateSessionFactory.beginTransaction();
			session.saveOrUpdate(countryPhoto);
			HibernateSessionFactory.commitTransaction();
		} catch (HibernateException e) {
			HibernateSessionFactory.rollbackTransaction();
			throw new RuntimeException(e.getMessage());
		} finally {
			// HibernateSessionFactory.closeSession();
		}

	}

	public void remove(CountryPhoto countryPhoto) {
		try {
			Session session = HibernateSessionFactory.currentSession();
			HibernateSessionFactory.beginTransaction();
			session.delete(countryPhoto);
			HibernateSessionFactory.commitTransaction();
			session.flush();
		} catch (Exception e) {
			HibernateSessionFactory.rollbackTransaction();
			throw new RuntimeException(e.getMessage());
		} finally {
			// HibernateSessionFactory.closeSession();
		}
	}

	public List find(String where) {
		List list = null;
		try {
			Session session = HibernateSessionFactory.currentSession();
			list = session.createQuery(
					" from CountryPhoto "  + " where " + where)
					.list();
		} catch (HibernateException e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			// HibernateSessionFactory.closeSession();
		}

		return list;
	}

	public Set initSet(Set set) {
		try {
			Hibernate.initialize(set);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			// HibernateSessionFactory.closeSession();
		}
		return set;
	}
    
   
}

